package com.ruoyi.production.service.impl;

import com.ruoyi.common.utils.SeqNumberUtils;
import com.ruoyi.production.domain.vo.CheckOutVo;
import com.ruoyi.production.domain.vo.ProductionExitVo;
import com.ruoyi.production.mapper.ProductionExitMapper;
import com.ruoyi.production.service.ProductionExitService;
import com.ruoyi.stock.domain.WarehouseIn;
import com.ruoyi.stock.domain.WarehouseInInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: YwaiX
 * @Version: 1.0
 * @Date: Created in 2024-11-04 10:36:51
 * @Description: 生产退料
 */

@Service
public class ProductionExitServiceImpl implements ProductionExitService {

    @Autowired
    private ProductionExitMapper productionExitMapper;

    @Override
    public List<ProductionExitVo> list() {
        return productionExitMapper.list();
    }

    @Override
    public List<CheckOutVo> checkout(String productionNo) {
        //获取出库单id
        Integer warehouseOutId = productionExitMapper.selectWarehouseOutId(productionNo);
        //根据出库单id查找出库的哪些物料
        return productionExitMapper.selectWarehouseOutInfo(warehouseOutId) ;
    }

    @Transactional
    @Override
    public Integer returnMaterial(CheckOutVo checkOutVo) {
        WarehouseIn warehouseIn = new WarehouseIn();
        warehouseIn.setWarehouseInNo(SeqNumberUtils.getSCTLNumber());
        warehouseIn.setSourceNo(checkOutVo.getSourceNo());
        warehouseIn.setGoodsType(3L);
        warehouseIn.setWarehouseInStatus(1L);
        productionExitMapper.insertWarehouseIn(warehouseIn);

        List<WarehouseInInfo> list = new ArrayList<>();
        for (CheckOutVo check : checkOutVo.getList()) {
            WarehouseInInfo warehouseInInfo = new WarehouseInInfo();
            warehouseInInfo.setWarehouseInId(warehouseIn.getId());
            warehouseInInfo.setGoodsNo(check.getGoodsNo());
            warehouseInInfo.setWarehouseInNumber(check.getNumber());
            list.add(warehouseInInfo);
        }
        return productionExitMapper.insertWarehouseInInfo(list);
    }
}
